package app.Insam.VNC;

import android.location.Location;

public class InsamLocationInfo {
	public double Latitude;
	public double Longitude;
	
	public InsamLocationInfo(double _Latitude, double _Longitude) {
		Latitude=_Latitude;
		Longitude=_Longitude;
	}

	public InsamLocationInfo(InsamLocationInfo _loc) {
		this(_loc.Latitude, _loc.Longitude);
	}
	
	public InsamLocationInfo(Location _loc) {
		this(_loc.getLatitude(), _loc.getLongitude());
	}
	
	public InsamLocationInfo(String _loc) {		
		int index = _loc.indexOf(",");
		if(index!=-1) {		
			Latitude = Double.parseDouble(_loc.substring(0, index));
			Longitude = Double.parseDouble(_loc.substring(index+1));
		} else {
			Latitude=0.0;
			Longitude=0.0;
		}
	}	
	
	
	public InsamLocationInfo SetLocation(double _Latitude, double _Longitude) {
		Latitude=_Latitude;
		Longitude=_Longitude;
		return this;
	}
	
	public InsamLocationInfo SetLocation(InsamLocationInfo _loc) {
		return SetLocation(_loc.Latitude, _loc.Longitude);
	}
	
	public InsamLocationInfo SetLocation(Location _loc) {
		return SetLocation(_loc.getLatitude(), _loc.getLongitude());
	}
	
//	private static boolean check(String value) {
//		boolean b = value.matches("[0-9]{1,3}(\\.[0-9]{1,9})?,[0-9]{1,3}(\\.[0-9]{1,9})?");
//	
//		Log.d(InsamMain.TAG, "check:"+b);	
//		
//		return b;
//	}
	
	public double CalcDistance(InsamLocationInfo from) {
		double kEarthRadiusKms = 6376.5;
        double dLat1InRad = Latitude * (Math.PI / 180.0);
        double dLong1InRad = Longitude * (Math.PI / 180.0);
        double dLat2InRad = from.Latitude * (Math.PI / 180.0);
        double dLong2InRad = from.Longitude * (Math.PI / 180.0);

        double dLongitude = dLong2InRad - dLong1InRad;
        double dLatitude = dLat2InRad - dLat1InRad;

        double a = Math.pow(Math.sin(dLatitude / 2.0), 2.0) + 
            Math.cos(dLat1InRad) * Math.cos(dLat2InRad) * 
            Math.pow(Math.sin(dLongitude / 2.0), 2.0);

        double c = 2000.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));            
        return (kEarthRadiusKms * c);
	}
	
//	public double  CalcDistance(Location from) {	     
//        return CalcDistance(new InsamLocationInfo(from));
//	}
	
	@Override
	public String toString() {		
		return String.format("geo:%f,%f", Latitude, Longitude);
	}	
}
